<?php

use Phalcon\Mvc\Model\Validator\Email as Email;

use Phalcon\Mvc\Model\Validator\Uniqueness as Uniqueness;

class UserAccountTb extends ModelBase
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var string
     */
    public $email;

    /**
     *
     * @var string
     */
    public $fname;

    /**
     *
     * @var string
     */
    public $mname;

    /**
     *
     * @var string
     */
    public $lname;

    /**
     *
     * @var string
     */
    public $bdate;

    /**
     *
     * @var string
     */
    public $gender;

    /**
     *
     * @var string
     */
    public $username;

    /**
     *
     * @var integer
     */
    public $activated;

    /**
     *
     * @var string
     */
    public $last_login;

    /**
     *
     * @var string
     */
    public $date_created;

    /**
     *
     * @var string
     */
    public $date_modified;

    /**
     *
     * @var string
     */
    public $password;

    /**
     *
     * @var string
     */
    public $profile_pic;

    /**
     *
     * @var string
     */
    public $activation_key;

    public function getProfilePicUrl($user_id)
    {
        $phql = 'SELECT profile_pic from UserAccountTb
                WHERE id = ?0';
        $data = $this->modelsManager->executeQuery($phql,array(0=>$user_id));
        return $data[0]->profile_pic;   
    }
    public function getFullname($user_id)
    {
        $phql = 'SELECT fname,lname from UserAccountTb
                WHERE id = ?0';
        $data = $this->modelsManager->executeQuery($phql,array(0=>$user_id));
        return $data[0]->fname.' '.$data[0]->lname;
    }
    public function validation()
    {

        $this->validate(
            new Email(
                array(
                    'field'    => 'email',
                    'required' => true,
                )
            )
        );
        if ($this->validationHasFailed() == true) {
            return false;
        }

        @$this->fname = $this->filter->sanitize($this->fname,'string');
        @$this->mname = $this->filter->sanitize($this->mname,'string');
        @$this->lname = $this->filter->sanitize($this->lname,'string');
        @$this->username = $this->filter->sanitize($this->username,'string');
        @$this->fname = @ucwords($this->fname);
        @$this->mname = @ucwords($this->mname);
        @$this->lname = @ucwords($this->lname);
        
    }
    public function beforeValidationOnCreate()
    {        

         $this->validate(new Uniqueness(
          array(
                "field"   => "email",
                "message" => "The email is already registered please try another"
            )
        ));

        $this->date_created = CURR_DATE;
        

        if ($this->validationHasFailed() == true)
        {
            return false;
        }
    }

    public function beforeValidationOnUpdate()
    {
        $this->date_modified = CURR_DATE;
    }

    /**
     * Independent Column Mapping.
     */
    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'email' => 'email', 
            'fname' => 'fname', 
            'mname' => 'mname', 
            'lname' => 'lname', 
            'bdate' => 'bdate', 
            'gender' => 'gender', 
            'username' => 'username', 
            'activated' => 'activated', 
            'last_login' => 'last_login', 
            'date_created' => 'date_created', 
            'date_modified' => 'date_modified', 
            'password' => 'password', 
            'profile_pic' => 'profile_pic', 
            'activation_key' => 'activation_key'
        );
    }

}
